package org.road0329.dlt698.axdr;


import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrNull;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;

public class RSD extends AxdrType {
    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private AxdrNull omd0 = null;
    private Selector1 s1 = null;
    private Selector2 s2 = null;
    private Selector3 s3 = null;
    private Selector4 s4 = null;
    private Selector5 s5 = null;
    private Selector6 s6 = null;
    private Selector7 s7 = null;
    private Selector8 s8 = null;
    private Selector9 s9 = null;
    private Selector10 s10 = null;

    public RSD() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public Choice getChoice() {
        return choice;
    }

    public AxdrNull getOmd0() {
        return omd0;
    }

    public Selector1 getS1() {
        return s1;
    }

    public Selector2 getS2() {
        return s2;
    }

    public Selector3 getS3() {
        return s3;
    }

    public Selector4 getS4() {
        return s4;
    }

    public Selector5 getS5() {
        return s5;
    }

    public Selector6 getS6() {
        return s6;
    }

    public Selector7 getS7() {
        return s7;
    }

    public Selector8 getS8() {
        return s8;
    }

    public Selector9 getS9() {
        return s9;
    }

    public Selector10 getS10() {
        return s10;
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int)choosen.getValue());
        if (this.choice == Choice.OMD0) {
            this.omd0 = new AxdrNull();
            codeLength += this.omd0.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.S1) {
            this.s1 = new Selector1();
            codeLength += this.s1.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.S2) {
            this.s2 = new Selector2();
            codeLength += this.s2.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.S3) {
            this.s3 = new Selector3();
            codeLength += this.s3.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.S4) {
            this.s4 = new Selector4();
            codeLength += this.s4.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.S5) {
            this.s5 = new Selector5();
            codeLength += this.s5.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.S6) {
            this.s6 = new Selector6();
            codeLength += this.s6.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.S7) {
            this.s7 = new Selector7();
            codeLength += this.s7.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.S8) {
            this.s8 = new Selector8();
            codeLength += this.s8.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.S9) {
            this.s9 = new Selector9();
            codeLength += this.s9.decode(input);
            return codeLength;
        }
        if (this.choice == Choice.S10) {
            this.s10 = new Selector10();
            codeLength += this.s10.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == Choice.S10) {
            codeLength += this.s10.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(10);
            codeLength += c.encode(output);
            return codeLength;
        }
        if (this.choice == Choice.S9) {
            codeLength += this.s9.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(9);
            codeLength += c.encode(output);
            return codeLength;
        }
        if (this.choice == Choice.S8) {
            codeLength += this.s8.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(8);
            codeLength += c.encode(output);
            return codeLength;
        }
        if (this.choice == Choice.S7) {
            codeLength += this.s7.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(7);
            codeLength += c.encode(output);
            return codeLength;
        }
        if (this.choice == Choice.S6) {
            codeLength += this.s6.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(6);
            codeLength += c.encode(output);
            return codeLength;
        }
        if (this.choice == Choice.S5) {
            codeLength += this.s5.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(5);
            codeLength += c.encode(output);
            return codeLength;
        }
        if (this.choice == Choice.S4) {
            codeLength += this.s4.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(4);
            codeLength += c.encode(output);
            return codeLength;
        }
        if (this.choice == Choice.S3) {
            codeLength += this.s3.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(3);
            codeLength += c.encode(output);
            return codeLength;
        }
        if (this.choice == Choice.S2) {
            codeLength += this.s2.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(2);
            codeLength += c.encode(output);
            return codeLength;
        }
        if (this.choice == Choice.S1) {
            codeLength += this.s1.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(1);
            codeLength += c.encode(output);
            return codeLength;
        }
        if (this.choice == Choice.OMD0) {
            codeLength += this.omd0.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.omd0 = null;
        this.s1 = null;
        this.s2 = null;
        this.s3 = null;
        this.s4 = null;
        this.s5 = null;
        this.s6 = null;
        this.s7 = null;
        this.s8 = null;
        this.s9 = null;
        this.s10 = null;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public void setOmd0(AxdrNull omd0) {
        resetChoices();
        this.choice = Choice.OMD0;
        this.omd0 = omd0;
    }

    public void setS1(Selector1 s1) {
        resetChoices();
        this.choice = Choice.S1;
        this.s1 = s1;
    }

    public void setS2(Selector2 s2) {
        resetChoices();
        this.choice = Choice.S2;
        this.s2 = s2;
    }

    public void setS3(Selector3 s3) {
        resetChoices();
        this.choice = Choice.S3;
        this.s3 = s3;
    }

    public void setS4(Selector4 s4) {
        resetChoices();
        this.choice = Choice.S4;
        this.s4 = s4;
    }

    public void setS5(Selector5 s5) {
        resetChoices();
        this.choice = Choice.S5;
        this.s5 = s5;
    }

    public void setS6(Selector6 s6) {
        resetChoices();
        this.choice = Choice.S6;
        this.s6 = s6;
    }

    public void setS7(Selector7 s7) {
        resetChoices();
        this.choice = Choice.S7;
        this.s7 = s7;
    }

    public void setS8(Selector8 s8) {
        resetChoices();
        this.choice = Choice.S8;
        this.s8 = s8;
    }

    public void setS9(Selector9 s9) {
        resetChoices();
        this.choice = Choice.S9;
        this.s9 = s9;
    }

    public void setS10(Selector10 s10) {
        resetChoices();
        this.choice = Choice.S10;
        this.s10 = s10;
    }

    @Override
    public String toString() {
        if (this.choice == Choice.OMD0) {
            if (this.omd0 != null) {
                return "choice: {omd0: " + this.omd0 + "}";
            } else {
                return "choice is OMD0 but omd0 is null";
            }
        } else if (this.choice == Choice.S1) {
            if (this.s1 != null) {
                return "choice: {s1: " + this.s1 + "}";
            } else {
                return "choice is s1 but s1 is null";
            }
        } else if (this.choice == Choice.S2) {
            if (this.s2 != null) {
                return "choice: {s2: " + this.s2 + "}";
            } else {
                return "choice is s2 but s2 is null";
            }
        } else if (this.choice == Choice.S3) {
            if (this.s3 != null) {
                return "choice: {s3: " + this.s3 + "}";
            } else {
                return "choice is s3 but s3 is null";
            }
        } else if (this.choice == Choice.S4) {
            if (this.s4 != null) {
                return "choice: {s4: " + this.s4 + "}";
            } else {
                return "choice is s4 but s4 is null";
            }
        } else if (this.choice == Choice.S5) {
            if (this.s5 != null) {
                return "choice: {s5: " + this.s5 + "}";
            } else {
                return "choice is s5 but s5 is null";
            }
        } else if (this.choice == Choice.S6) {
            if (this.s6 != null) {
                return "choice: {s6: " + this.s6 + "}";
            } else {
                return "choice is s6 but s6 is null";
            }
        } else if (this.choice == Choice.S7) {
            if (this.s7 != null) {
                return "choice: {s7: " + this.s7 + "}";
            } else {
                return "choice is s7 but s7 is null";
            }
        } else if (this.choice == Choice.S8) {
            if (this.s8 != null) {
                return "choice: {s8: " + this.s8 + "}";
            } else {
                return "choice is s8 but s8 is null";
            }
        } else if (this.choice == Choice.S9) {
            if (this.s9 != null) {
                return "choice: {s9: " + this.s9 + "}";
            } else {
                return "choice is s9 but s9 is null";
            }
        } else if (this.choice == Choice.S10) {
            if (this.s10 != null) {
                return "choice: {s10: " + this.s10 + "}";
            } else {
                return "choice is s10 but s10 is null";
            }
        } else {
            return "unknown";
        }
    }

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        OMD0(0),
        S1(1),
        S2(2),
        S3(3),
        S4(4),
        S5(5),
        S6(6),
        S7(7),
        S8(8),
        S9(9),
        S10(10);

        private final int value;

        private Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}



